/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package uit.ejb.dae.phong;

import com.sun.rowset.WebRowSetImpl;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.FinderException;
import javax.sql.rowset.WebRowSet;
import uit.ejb.connection.ConnectingSQL;
import uit.ejb.dto.PhongDTO;

/**
 *
 * @author Restart
 */
public class PhongDAE implements EntityBean {

    private EntityContext context;
    public static int flag = -1;
    
    // <editor-fold defaultstate="collapsed" desc="EJB infrastructure methods. Click the + sign on the left to edit the code.">

    // TODO Add code to acquire and use other enterprise resources (DataSource, JMS, enterprise beans, Web services)
    // TODO Add business methods
    // TODO Add create methods

    /**
     * @see javax.ejb.EntityBean#setEntityContext(javax.ejb.EntityContext)
     */
    public void setEntityContext(EntityContext aContext) {
        context = aContext;
    }
    
    /**
     * @see javax.ejb.EntityBean#ejbActivate()
     */
    public void ejbActivate() {
        
    }
    
    /**
     * @see javax.ejb.EntityBean#ejbPassivate()
     */
    public void ejbPassivate() {
        
    }
    
    /**
     * @see javax.ejb.EntityBean#ejbRemove()
     */
    public void ejbRemove() {
        
    }
    
    /**
     * @see javax.ejb.EntityBean#unsetEntityContext()
     */
    public void unsetEntityContext() {
        context = null;
    }
    
    /**
     * @see javax.ejb.EntityBean#ejbLoad()
     */
    public void ejbLoad() {
        // TODO add code to retrieve data
    }
    
    /**
     * @see javax.ejb.EntityBean#ejbStore()
     */
    public void ejbStore() {
        // TODO add code to persist data
    }

    // </editor-fold>
    
    /**
     * See EJB 2.0 and EJB 2.1 section 12.2.5
     */
    public java.lang.String ejbFindByPrimaryKey(java.lang.String aKey) throws FinderException {
        // TODO add code to locate aKey from persistent storage
        // throw javax.ejb.ObjectNotFoundException if aKey is not in
        // persistent storage.
        return "";
    }

    public boolean kiemTraTrung(PhongDTO phong) {
        try{
            Connection _con = ConnectingSQL.makeConnection();
            CallableStatement cs = _con.prepareCall("{call sp_Phong_CheckName(?,?)}");
            cs.setString(1, phong.getTenPhong().toString());
            cs.setString(2, phong.getMaLoaiPhong().toString());
            ResultSet rs = cs.executeQuery();
            if(flag == 0)
            {
                if(rs.next())
                    return true;
            }
            if(flag == 1)
            {
                while(rs.next())
                {
                    if(!rs.getObject("MaPhong").toString().equals(phong.getMaPhong().toString()))
                        return true;
                }
            }
        }catch(Exception e){
            e.getMessage();
        }
        return false;
    }

    public boolean phong_Insert(PhongDTO phong) {
        flag = 0;
        if(this.kiemTraTrung(phong))
        {
            flag = -1;
            return false;
        }
        else
        {
            try{
                Connection _con = ConnectingSQL.makeConnection();
                CallableStatement cs = _con.prepareCall("{call sp_Phong_Insert(?,?,?,?)}");
                cs.setString(1, phong.getTenPhong());
                cs.setString(2, phong.getMaLoaiPhong());
                cs.setString(3, phong.getTinhTrang());
                cs.setString(4, phong.getGhiChu());
                cs.executeUpdate();
                flag = -1;
                return true;
            }
            catch(Exception e)
            {
                e.getMessage();
                return false;
            }
        }
    }

    public boolean phong_Update(PhongDTO phong) {
        flag = 1;
        if(this.kiemTraTrung(phong))
        {
            flag = -1;
            return false;
        }
        else
        {
            try{
                Connection _con = ConnectingSQL.makeConnection();
                CallableStatement cs = _con.prepareCall("{call sp_Phong_Update(?,?,?,?,?)}");
                cs.setString(1, phong.getMaPhong());
                cs.setString(2, phong.getTenPhong());
                cs.setString(3, phong.getMaLoaiPhong());
                cs.setString(4, phong.getTinhTrang());
                cs.setString(5, phong.getGhiChu());
                cs.executeUpdate();
                flag = -1;
                return true;
            }
            catch(Exception e)
            {
                e.getMessage();
                return false;
            }
        }
    }

    public boolean phong_Delete(String id) {
        try{
            Connection _con = ConnectingSQL.makeConnection();
            CallableStatement cs = _con.prepareCall("{call sp_Phong_Delete(?)}");
            cs.setString(1, id);
            cs.executeUpdate();
            return true;
        }
        catch(Exception e)
        {
            e.getMessage();
            return false;
        }
    }

    public WebRowSet phong_GetByAll() {
        try{
            Connection _con = ConnectingSQL.makeConnection();
            CallableStatement cs = _con.prepareCall("{call sp_Phong_GetByAll}");
            ResultSet rs = cs.executeQuery();
            WebRowSet wrs = new WebRowSetImpl();
            wrs.populate(rs);
            return wrs;
        }
        catch(Exception e)
        {
            e.getMessage();
            return null;
        }
    }

    public WebRowSet phong_GetById(String id) {
        try
        {
            Connection _con = ConnectingSQL.makeConnection();
            CallableStatement cs = _con.prepareCall("{call sp_Phong_GetById(?)}");
            cs.setString(1, id);
            WebRowSet wr = new WebRowSetImpl();
            wr.populate(cs.executeQuery());
            return wr;
        }
        catch(Exception e)
        {
            e.getMessage();
            return null;
        }
    }

    public WebRowSet phong_GetByTop(String top, String where, String order) {
        try
        {
            Connection _con = ConnectingSQL.makeConnection();
            CallableStatement cs = _con.prepareCall("{call sp_Phong_GetByTop(?,?,?)}");
            cs.setString(1, top);
            cs.setString(2, where);
            cs.setString(3, order);
            WebRowSet wrs = new WebRowSetImpl();
            wrs.populate(cs.executeQuery());
            return wrs;
        }
        catch(Exception e)
        {
            e.getMessage();
            return null;
        }
    }

    public void phong_UpdateStatus(String id, String status) {
        try
        {
            Connection _con = ConnectingSQL.makeConnection();
            CallableStatement cs = _con.prepareCall("{call sp_Phong_UpdateStatus(?,?)}");
            cs.setString(1, id);
            cs.setString(2, status);
            cs.executeUpdate();
        }
        catch(Exception e)
        {
        }
    }

}
